/**
	@author Matthew Richmond
	@version 2005-08-12
 	@usage
	<code>
		test = new BezierTweenController(this.ball_mc, 3, true, [50, 50], [50, 250] , [250, 250], [250, 50], 1)
	</code>
*/

import mx.transitions.Tween;
import mx.transitions.easing.*;
import com.airtightinteractive.BezierTween;

class timber.transitions.BezierTweenController {

	private var testMode:Boolean = false;
	var obj:MovieClip;
	var duration:Number;
	var useSeconds:Boolean;
	var sX:Number;
	var sY:Number;
	var scX:Number;
	var scY:Number;
	var sColor:String = "0x006600";
	var eX:Number;
	var eY:Number;
	var ecX:Number;
	var ecY:Number;
	var eColor:String = "0xFF0000";
	var delay:Number;
	var callbackObj:MovieClip;
	var callback:String;
	
	private var delayTimer:Number;

	function BezierTweenController(obj, duration, useSeconds, startXY, startControlXY , endXY, endControlXY, delay, callbackObj, callback){
		
		this.obj = obj;
		this.duration = duration;
		this.useSeconds = useSeconds;
	
		this.sX = startXY[0];
		this.sY = startXY[1];
	
		this.scX = startControlXY[0];
		this.scY = startControlXY[1];
		this.eX = endXY[0];
		this.eY = endXY[1];
		this.ecX = endControlXY[0];
		this.ecY = endControlXY[1];
	
		if (delay != undefined) {
			this.delay = delay;
		};
	
		if (callbackObj != undefined) {
			this.callbackObj = callbackObj;
			this.callback = callback
		};
	
		if (testMode == true) {
			this.buildHandle("s");
			this.buildHandle("e");
		};
	
		this.delayTimer = setInterval ((mx.utils.Delegate.create(this, goTween)), (this.delay*1000) );
	};
	
	
	function buildHandle( point:String ){
		//trace("buildHandle");
		var local:MovieClip = this.obj._parent;
		var hColor:String = this[point+"Color"];
	
		var line:MovieClip = local.createEmptyMovieClip(point+"Line_mc", local.getNextHighestDepth());
		with ( line ) {
			lineStyle(1,hColor,30);
			moveTo(this[point+"X"], this[point+"Y"]);
			lineTo(this[point+"cX"], this[point+"cY"]);
		};
		
		var reg:MovieClip = local.createEmptyMovieClip(point+"_mc", local.getNextHighestDepth(), {_x:this[point+"X"] , _y:this[point+"Y"]});
		var regSize:Number = 3;
		with ( reg ) {
			lineStyle(1,hColor,30);
			moveTo(this[point+"X"], this[point+"Y"]-regSize);
			lineTo(this[point+"X"], this[point+"Y"]+regSize);
			
			moveTo(this[point+"X"]-regSize, this[point+"Y"]);
			lineTo(this[point+"X"]+regSize, this[point+"Y"]);
		};
		
		var handleX:Number = this[point+"cX"];
		var handleY:Number = this[point+"cY"];
		var handle:MovieClip = local.createEmptyMovieClip(point+"C_mc", local.getNextHighestDepth(), {_x:handleX, _y:handleX });
		var handleSize:Number = 3;
		with ( handle ) {
			lineStyle(1,hColor,30);
			beginFill(0xCCCCCC);
			moveTo(this[point+"cX"]-regSize, this[point+"cY"]-regSize);
			lineTo(this[point+"cX"]+regSize, this[point+"cY"]-regSize);
			lineTo(this[point+"cX"]+regSize, this[point+"cY"]+regSize);
			lineTo(this[point+"cX"]-regSize, this[point+"cY"]+regSize);
			lineTo(this[point+"cX"]-regSize, this[point+"cY"]-regSize);
		};
		
		/*
		handle.onPress = function (){
			trace(this._x);
			this.startDrag();
			this._parent[point+"Line_mc"]._alpha = 30;
		};
		
		handle.onRelease = function (){
			trace(this._x);
			this.stopDrag();
			this._parent[point+"Line_mc"]._alpha = 100;
		};
		*/
		
	};
		
	function goTween(){
		//trace("goTween from "+this);
		clearInterval( this.delayTimer );
		var twnBezX = new BezierTween(this.obj, "_x", this.sX, this.eX, this.duration, this.useSeconds, this.scX, this.ecX);
		var twnBezY = new BezierTween(this.obj, "_y", this.sY, this.eY, this.duration, this.useSeconds, this.scY, this.ecY);
		
		twnBezX.callbackObj = this.callbackObj;
		twnBezX.callback = this.callback;
		
		twnBezX.onMotionFinished = function(){
			//trace("MOTION FINISHED");
			this.callbackObj[this.callback]();
		};
		
		
	};

};